<!--
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-29 20:00:26
 * @Description  : 基础设置页面
-->

<template>
  <div
    class="base-settings-container bg-lightColor rounded-sm px-[24px] pb-[24px] min-w-[640px]"
    v-loading="loading"
  >
    <n-tabs
      type="line"
      animated
      tab-class="my-test"
      :value="activeName"
      @update:value="handleActiveNameChange"
    >
      <n-tab-pane name="base" tab="基础设置" display-directive="show">
        <base-settings
          :settings="settings"
          :menu-show="menuShow"
          @change-settings="handleChangeSettings"
        ></base-settings>
      </n-tab-pane>
      <n-tab-pane name="safe" tab="安全设置" display-directive="show">
        <safe-settings
          :settings="settings"
          :menu-show="menuShow"
          @change-settings="handleChangeSettings"
        ></safe-settings>
      </n-tab-pane>
      <n-tab-pane name="denlu" tab="注册与登录" display-directive="show">
        <login-settings
          :settings="settings"
          :menu-show="menuShow"
          @change-settings="handleChangeSettings"
        ></login-settings>
      </n-tab-pane>
      <n-tab-pane name="upload" tab="上传与下载" display-directive="show">
        <upload-settings
          ref="uploadRef"
          :settings="settings"
          :menu-show="menuShow"
          @change-settings="handleChangeSettings"
        ></upload-settings>
      </n-tab-pane>
      <n-tab-pane name="email" tab="邮箱设置" display-directive="show">
        <email-settings
          :settings="settings"
          :menu-show="menuShow"
          @change-settings="handleChangeSettings"
        ></email-settings>
      </n-tab-pane>
      <n-tab-pane name="other" tab="其他设置" display-directive="show">
        <other-settings
          :settings="settings"
          :menu-show="menuShow"
          @change-settings="handleChangeSettings"
        ></other-settings>
      </n-tab-pane>
    </n-tabs>

    <n-divider class="!my-2"></n-divider>
    <n-button
      v-permission="[PERMISSIONS.ADMIN_SETTING]"
      type="primary"
      class="grent-btn w-[120px] ml-[200px]"
      @click="handleSaveSettings"
    >
      保存
    </n-button>
  </div>
</template>

<script setup lang="ts">
import { useMessage } from 'naive-ui'
import router from '@/router'
import { getSystemOptionList, updateSystemOption } from '@/api/systemOption'
import type { IndexObj } from '@/types/commom'
import { load } from '@/utils/loading'
import { PERMISSIONS } from '@/constants/permission'
import { userMenuShow } from '@/hooks/useMenuShow'
import BaseSettings from './components/BaseSettings.vue'
import SafeSettings from './components/SafeSettings.vue'
import LoginSettings from './components/LoginSettings.vue'
import UploadSettings from './components/UploadSettings.vue'
import EmailSettings from './components/EmailSettings.vue'
import OtherSettings from './components/OtherSettings.vue'

const message = useMessage()
const route = useRoute()
const { menuShow } = userMenuShow()

const activeName = ref('base')
const loading = ref(false)

const handleActiveNameChange = (value: string) => {
  activeName.value = value
  router.push({
    name: 'settings',
    params: {
      page: value
    }
  })
}

// 系统配置项
const settings = ref<IndexObj>({
  companyName: '我的网盘',
  companySubName: '产品副标题',
  ingoreSources: '.git',
  newUserDefaultSources: '我的音乐,我的图片,我的文档',
  pwdLock: '1',
  loginValidateCode: '1',
  msgValidateCode: '1',
  pwdLevel: '0',
  isLinkShow: '1',
  isLink: '1',
  isLinkPwdAllowEmpty: '1',
  isLinkAllowNotLogin: '1',
  isLinkPackDownload: '1',
  sourceMd5Show: '1',
  thirdPartLogin: 'qq,weixin,github',
  userRegister: '1',
  userRegisterCheck: '0',
  defaultSizeMax: '10737418240', // 这里是字节数量
  defaultRoleId: '3',
  defaultDepInfo: '[]',
  chunkSize: '20971520',
  uploadThreadNum: '10',
  requestRetryNum: '5',
  uploadIngoreSources: '',
  emailType: '0',
  emailServer: '',
  emailAddr: '',
  emailEncrpyType: '0',
  emailAuthPwd: '',
  dataFormat: 'YYYY-MM-DD'
})

const uploadRef = ref()

// 保存系统配置到数据库
const handleSaveSettings = () => {
  // 上传下载需要进行验证
  uploadRef.value.formRef.validate((error: any) => {
    if (error) {
      handleActiveNameChange('upload')
      message.error(error[0][0].message)
      return
    }
    // 进行配置的保存
    load.show()
    updateSystemOption(settings.value)
      .then(() => {
        message.success('操作成功')
      })
      .catch(console.error)
      .finally(() => {
        load.hide()
      })
  })
}

const loadSettings = () => {
  loading.value = true
  getSystemOptionList()
    .then((res) => {
      settings.value = res.data
    })
    .catch(console.error)
    .finally(() => {
      loading.value = false
    })
}

onMounted(() => {
  loadSettings()
  activeName.value = (route.params.page as string) || 'base'
})

const handleChangeSettings = (key: string, value: string) => {
  settings.value[key] = value
}
</script>

<style scoped lang="scss"></style>
